package com.paper.parser.core;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author Corleone
 * @desc:
 * @date 2018-10-11 10:23:23
 */
@Service
public abstract class BaseParser implements IParser {


    protected abstract Map<String, Object> getMessage();


    /* 按照不同的规则解析
     * @param context 上下文信息，有些信息是在上下文中得到的，比如cookie
     */
    protected abstract Map<String, Object> parseContent(Map<String, Object> context);

    @Override
    public Map<String, Object> getParseTask() {
        Map<String, Object> context = getMessage();
        String pageIndex = (String) context.get("pageIndex");
        String page = redisClient.get(pageIndex);
        if (StringUtils.isEmpty(page)) {
            crawlAgain(context);
        }
        context.put("page", page);
        return context;
    }

    @Override
    public void parse() {
        Map<String, Object> context = getParseTask();
        if (isRobotPage(context)) {
            context.remove("proxy");
            crawlAgain(context);
        }
        Map<String, Object> content = parseContent(context);
        context.put("content", content);
        save(context);
        addSpiderTask(context);
    }

    @Override
    public boolean addSpiderTask(Map<String, Object> context) {
        return false;
    }

    @Override
    public void crawlAgain(Map<String, Object> context) {
        int nextTriedTime = (Integer) context.getOrDefault("triedTime", 0) + 1;
        context.put("triedTime", nextTriedTime);
        String msg = JSON.toJSONString(context);
        sendMessage(msg);
    }

}
